import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const initDesc = formatMessage({
    id: 'api.message.initDesc',
    defaultMessage: '初始化界面'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件备料单'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件备料单详情'
});
const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出备件备料单'
});
const confirmorDataDesc = formatMessage({
    id: 'api.message.confirmorDataDesc',
    defaultMessage: '确认'
});
const closeDataDesc = formatMessage({
    id: 'api.message.closeDataDesc',
    defaultMessage: '关闭确认'
});
const importApproveTempDesc = formatMessage({
    id: 'api.message.importApproveTempDesc',
    defaultMessage: '批量审核导入模板'
});
const importTempDesc = formatMessage({
    id: 'api.message.importTempDesc',
    defaultMessage: '预计到货信息导入模板'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/partBackOrder/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, initDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partBackOrders/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 编辑查询订单列表
export const queryList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partBackOrders?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partBackOrders/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

//确认 申请取消或批量取消
export const confirmorData = data => request(`${baseUrl}/partBackOrders/confirmor`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmorDataDesc));

//关闭
export const colseData = data => request(`${baseUrl}/partBackOrders/colse`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, closeDataDesc));
// 导出
export const exportPartIns = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partBackOrders/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDesc))
        .then(res => res.data);
};
// 批量审核导入模板
export const importApproveTemp = () => request(`${baseUrl}/partBackOrders/approve/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importApproveTempDesc)).then(res => res.data);

// 预计到货信息导入模板
export const importTemp = () => request(`${baseUrl}/partBackOrders/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
